package me.zhengjie.modules.weixin.callback;

import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import me.zhengjie.constant.ConstantUtils;
import me.zhengjie.modules.system.domain.ExamOrder;
import me.zhengjie.modules.system.domain.ExamResult;
import me.zhengjie.modules.system.service.ExamOrderService;
import me.zhengjie.modules.web.service.mybatisplus.IExamResultPlusService;
import me.zhengjie.utils.enums.OrderPrefixEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;

@Service
public class ReportCallbackServiceImpl implements IPayCallbackService{
    private static final Logger log = LoggerFactory.getLogger(ReportCallbackServiceImpl.class);
    @Autowired
    private ExamOrderService orderService;

    @Autowired
    private IExamResultPlusService examResultService;
    @Override
    public OrderPrefixEnum prefix() {
        return OrderPrefixEnum.Report;
    }

    @Override
    public boolean execute(WxPayOrderNotifyResult notifyResult) {
        ExamOrder one = orderService.findOne(notifyResult.getOutTradeNo());
        if (one.getOrderAmount().multiply(BigDecimal.valueOf(100)).compareTo(BigDecimal.valueOf(notifyResult.getTotalFee())) != 0){
            log.error("支付金额和订单金额不一致");
            return false;
        }
        one.setPayAmount(new BigDecimal(notifyResult.getTotalFee()).divide(new BigDecimal(100)));
        one.setPaySerialid(notifyResult.getTransactionId());
        one.setPayStatus(ConstantUtils.Order_Payed);
        one.setOrderStatus(ConstantUtils.Order_Payed);
        one.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        orderService.update(one);

        ExamResult examResult = new ExamResult();
        examResult.setId(one.getResultId());
        examResult.setOrderNo(notifyResult.getOutTradeNo());
        examResultService.updateById(examResult);
        return true;
    }
}
